<?php  

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class cancel_sales extends CI_Controller {
	function __construct()
	{
	parent::__construct();
		
		// check if salesman logged in 
		if (!$this->ion_auth->logged_in())
	  	{
			redirect('auth/login');
	  	}		
		$this->load->library('form_validation');
		$this->load->model('sales/sales_model');
		$this->load->model('sales/cancelsales_model');
		$this->load->model('purchase/purchase_model');
		//$this->load->model('people/salesman_model');		
		$this->load->library('ion_auth');
		$this->load->library('session');
		
		$this->load->library('form_validation');
		$this->load->helper('url');
			$this->load->library('datatables');
		// Load MongoDB library instead of native db driver if required
		$this->config->item('use_mongodb', 'ion_auth') ?
		$this->load->library('mongo_db') :

		$this->load->database();

		$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));

	}
	


	function add_sales(){
		if (!$this->ion_auth->logged_in())
			{
				redirect('auth', 'refresh');
			}
			
	$sales_product = $this->sales_model->edit_products($this->input->post('id_sale'))->result_array();
	$sales = $this->sales_model->edit_sales($this->input->post('id_sale'))->row_array();
			//print("<pre>");
			//print_r($sales);	
			//print_r($sales_product);
		
			if($this->input->post('trans_name')!=''){
				
				$trans_name = $this->input->post('trans_name');
				}else{
					$trans_name ='';
				}
				
			
			if($this->input->post('trans_amt')!=''){
				
				$trans_amt = $this->input->post('trans_amt');
				}else{
					$trans_amt =0;
				}
			$id_customer = $this->input->post('id_customer');
			if($id_customer!='0'){
				$cancel_data = array(
				'new_invoice_no' =>strtoupper( $this->input->post('invoice_no')),
				'invoice_no' => $this->sales_model->old_invoiceno($this->input->post('id_sale')),
				'id_sales' => $this->input->post('id_sale'),
				'invoice_type' => $this->input->post('invoice_type'),
				'id_customer' => $id_customer,
				'customer_name' => strtoupper($this->sales_model->get_cname($id_customer)),
				'amount' => $this->input->post('total_amount'),
				'total_disc' => $this->input->post('total_discount'),
				'total_tax' => $this->input->post('total_tax'),
				
				'round_off' => $this->input->post('round_off'),
				'previous_total' => $sales['total_price'],
				'cancel_total' => $this->input->post('total_price'),
				'new_total' =>  floatval($sales['total_price']) - floatval($this->input->post('total_price')),
				'transaction_type' => $this->input->post('type'),
				'billed_date' => strtoupper($this->input->post('date')),
				'date' => $this->input->post('cancel_date'),
				'id_branch' => $sales['id_branch'],
				'cancel_by' => $this->session->userdata('username')
				);
				$sales_data = array(
				'invoice_no' => strtoupper($this->input->post('invoice_no')),
				'invoice_type' => $sales['invoice_type'],
				'date' => $this->input->post('cancel_date'),	
				'id_customer' => $id_customer,
				'customer_name' =>strtoupper( $this->sales_model->get_cname($id_customer)),
				'amount' => floatval($sales['amount']) - floatval($this->input->post('total_amount')),
				'total_disc' =>  floatval($sales['total_disc']) - floatval($this->input->post('total_discount')),
				'total_tax' => floatval($sales['total_tax'])- floatval($this->input->post('total_tax')),
			
				'round_off' => $this->input->post('round_off'),
				'total_price' => floatval($sales['total_price']) - floatval($this->input->post('total_price')),
				'transaction_type' => $this->input->post('type'),
				'transport_name' => strtoupper($sales['transport_name']),	
				'transport_amt' => $sales['transport_amt'],
				'final_price' => 	floatval($sales['total_price']) - floatval($this->input->post('total_price')),
				'id_branch' => $sales['id_branch'],
				'billed_by' => strtoupper($this->session->userdata('username'))
				);	
				
				//print_r($data);exit;
				//print_r($data);exit;
			}else{
				$cencel_data = array(
				'new_invoice_no' =>strtoupper( $this->input->post('invoice_no')),
				'id_sales' => $this->input->post('id_sale'),
				'invoice_no' =>strtoupper($this->sales_model->old_invoiceno($this->input->post('id_sale'))),
				'invoice_type' => $this->input->post('invoice_type'),
				'id_customer' => $this->input->post('id_customer'),
				'customer_name' => strtoupper($this->sales_model->get_cname($this->input->post('id_customer'))),
				'amount' => $this->input->post('total_amount'),
				'total_disc' => $this->input->post('total_discount'),
				'total_tax' => $this->input->post('total_tax'),
				'round_off' => $this->input->post('round_off'),
				'previous_total' => $sales('total_price'),
				'cancel_total' => $this->input->post('total_price'),
				'new_total' => $this->input->post('total_price'),
				'transaction_type' => $this->input->post('type'),
				'date' => $this->input->post('cancel_date'),
				'billed_date' => $this->input->post('date'),
				'transport_name' =>strtoupper($trans_name),	
				'transport_amt' => $trans_amt,
				'final_price' => floatval($this->input->post('total_price')),
				'id_branch' => $sales['id_branch'],
				'cancel_by' => strtoupper($this->session->userdata('username'))
				
			);
			$sales_data = array(
				'invoice_no' => strtoupper($this->input->post('invoice_no')),
				'invoice_type' => $sales['invoice_type'],
				'date' => $this->input->post('cancel_date'),	
				'id_customer' => $id_customer,
				'customer_name' =>strtoupper( $this->sales_model->get_cname($id_customer)),
				'amount' => floatval($sales['amount']) - floatval($this->input->post('total_amount')),
				'total_disc' =>  floatval($sales['total_disc']) - floatval($this->input->post('total_discount')),
				'total_tax' => floatval($sales['total_tax'])- floatval($this->input->post('total_tax')),
				'round_off' => $this->input->post('round_off'),
				'total_price' => floatval($sales['total_price']) - floatval($this->input->post('total_price')),
				'transaction_type' => $this->input->post('type'),
				'transport_name' => strtoupper($sales['transport_name']),	
				'transport_amt' => $sales['transport_amt'],
				'final_price' => 	floatval($sales['total_price']) - floatval($this->input->post('total_price')),
				'id_branch' => $sales['id_branch'],
				'billed_by' =>strtoupper( $this->session->userdata('username'))
				);	
		
	}
				///print("<pre>");
				//print_r($cancel_data);
				//print_r($sales_data);
				
				$id_cancel = $this->cancelsales_model->insert_cancel($cancel_data);
				$id_sale = $this->cancelsales_model->insert_sales($sales_data);
				//echo $id_cancel."//".$id_sale;
				$code = $this->input->post('code');
				$rate = $this->input->post('rate');
				$quantity = $this->input->post('quantity');
				$amount = $this->input->post('amount');
				$discount = $this->input->post('discount');
				$tax = $this->input->post('tax');
				
				$total = $this->input->post('total');
				$counter = 0;
				foreach($sales_product as $pp){
					$cancel_data1['id_cancel'] = $id_cancel;
					$cancel_data1['id_product'] = $code[$counter]; 
					$cancel_data1['previous_quantity'] = $pp['quantity'];
					$cancel_data1['cancel_quantity'] = $quantity[$counter];
					$cancel_data1['new_quantity'] = intval($pp['quantity']) - intval($quantity[$counter]);
					$cancel_data1['rate'] = $rate[$counter];
					$cancel_data1['amount'] = $amount[$counter];
					$cancel_data1['discount'] = $discount[$counter];
					
					$cancel_data1['previous_total'] = $pp['total'];
					$cancel_data1['cancel_total'] = $total[$counter];
					$cancel_data1['new_total'] =floatval($pp['total']) - $total[$counter];
					$cancel_data1['date'] = $cancel_data['billed_date'];
					
					$sales_data1['id_sale'] = $id_sale;
					$sales_data1['id_product'] = $pp['id_product']; 
					$sales_data1['quantity'] = intval($pp['quantity']) - intval($quantity[$counter]);
					$sales_data1['rate'] = $rate[$counter];
					$sales_data1['amount'] = floatval($pp['amount']) - floatval($amount[$counter]);
			
					$sales_data1['discount'] = floatval($discount[$counter]);
					$sales_data1['tax'] = floatval($tax[$counter]);
					$cancel_data1['tax'] = $tax[$counter];
					$sales_data1['total'] =  floatval($pp['total']) - $total[$counter];
					$sales_data1['date'] = $cancel_data['date'];
					$this->cancelsales_model->insert_cancelproduct($cancel_data1,$sales_data1,$sales['id_branch']);
					$counter = $counter+1;				
				}
				

				
		redirect("sales/cancel_sales/view_cancelsales");	
		//print_r($total);exit;
	}	


	function view_cancelsales(){
	   $this->data['users'] = $this->sales_model->get_salesList()->result();
		//print_r($this->data['users']);exit;
	   $meta['page_title'] = "Sales";
	   $this->data['message'] = "Sales";
	   $this->load->view('common/header', $meta);
	   $this->load->view('sales/view_cancelsales');
       $this->load->view('common/footer');	
	
}
function get(){
		echo $this->cancelsales_model->get();
	}



	
function report($id){
	$meta['page_title'] = 'Cancelsales Report';
	$sales_man=$this->session->userdata('username');
	//$this->data['sales_man']= $sales_man;
	$sales = $this->cancelsales_model->getcancel_sales($id)->row_array();
	$this->data['sales']= $sales;
	$this->data['profile'] = $this->sales_model->get_company_profile()->row_array();
	$customer = $this->sales_model->get_customer_details($sales['id_customer'])->row_array();
	//print("<pre>");
	if(count($customer)> 0){
			$this->data['customer']= $customer;
			$this->data['invoice_no']=$sales['invoice_type'];
			$this->data['referance_no']=$sales['invoice_no'];
			$this->data['products']= $this->cancelsales_model->get_uniquesale($id)->result();
	//	print_r($this->data);exit;
			$meta['title']='Sales Report';
			$this->load->view('common/header', $meta);
		   $this->load->view('sales/cancelsale_report',$this->data);
	       $this->load->view('common/footer');	
	}else{
		
			$this->data['invoice_no']=$sales['invoice_type'];
			$this->data['referance_no']=$sales['invoice_no'];
			$this->data['products']= $this->cancelsales_model->get_uniquesale($id)->result();
			$meta['title']='Sales Report';
			$this->load->view('common/header', $meta);
		   	$this->load->view('sales/cancelsale_report1',$this->data);
	       	$this->load->view('common/footer');	

		
	}
}	



	
}

?>